let dbUtil = require('db-util')
const uniID = require('uni-id')
let db = uniCloud.database()
let dbCmd = db.command
let $ = dbCmd.aggregate
const errParams = {
	errCode: 400,
	errMsg: '参数错误！'
}
const errResNotFound = {
	errCode: 501,
	errMsg: '实体不存在！'
}
const moment = require('moment-timezone')
const todayStart = moment().hour(0).minute(0).second(0).millisecond(0).toDate().getTime()
const todayEnd = moment().hour(23).minute(59).second(59).millisecond(999).toDate().getTime()
module.exports = {
	_before: async function() { // 通用预处理器
		const token = this.getUniIdToken()
		if (!token) {
			throw new Error('未登录')
		}
		const payload = await uniID.checkToken(token)
		let {
			code,
			uid,
			permission
		} = payload
		if (code) {
			throw new Error('未登录')
		}
		this.uid = uid
		if (!permission) {
			permission = await uniID.getPermissionByUid({
				uid
			})
		}

		this.permission = permission
		let roleRes = await uniID.getRoleByUid({
			uid
		})
		if (roleRes.code != 0) {
			throw new Error(roleRes.msg)
		}
		this.roles = roleRes.role
		const methodName = this.getMethodName()
		this.isAdmin = this.roles.some(item => item == 'admin')
		this.isBoss = this.roles.some(item => item == 'boss')
		this.isStoreMgr = this.roles.some(item => item == 'boss' || item == 'employee' || item ==
			'script_keyboarder')
		if (methodName == 'getQrcode' && !this.isStoreMgr && !this.isAdmin) {
			throw new Error('无权限')
		}
		if (methodName == 'revert' && this.permission.indexOf('storeSign/revert') == -1 && !this.isAdmin) {
			throw new Error('无权限')
		}
		if (methodName == 'modifyIntegral' && this.permission.indexOf('storeSign/modifyIntegral') == -1 && !this
			.isAdmin) {
			throw new Error('无权限')
		}
		if (methodName == 'remove' && this.permission.indexOf('storeSign/remove') == -1 && !this.isAdmin) {
			throw new Error('无权限')
		}
	},

	async modifyPlayerInfo({
		id,
		uid,
		note,
		role,
		rate
	}) {
		if (rate < 1) {
			return {
				errCode: 503,
				errMsg: "最低打一分"
			}
		}


		let signInfo = await dbUtil.getById({
			collection: 't3ds-store-sign',
			id,
			field: {
				signDetail: true,
				createBy: true,
				scriptId: true,
				storeId: true
			}
		})
		if (!signInfo) {
			return errResNotFound
		}
		if (signInfo.createBy != this.uid && !this.isAdmin) {
			let storeInfo = await dbUtil.getOne({
				collection: 't3ds-store',
				where: {
					_id: signInfo.storeId
				}
			})
			if (!storeInfo) {
				return {
					errCode: 503,
					errMsg: "无权限."
				}
			}
			if (storeInfo.uid != this.uid) {
				return {
					errCode: 503,
					errMsg: "无权限."
				}
			}
		}
		if (signInfo.signDetail) {
			for (var i = 0; i < signInfo.signDetail.length; i++) {
				let d = signInfo.signDetail[i]
				if (d.uid == uid) {

					let transaction = await db.startTransaction()
					var data = {}
					if (rate == null) {
						data = {
							_id: id,
							['signDetail.' + i]: {
								note,
								role
							}
						}
					} else {
						data = {
							_id: id,
							['signDetail.' + i]: {
								note,
								role,
								rate
							}
						}
					}

					try {
						//查询是否已经评价过 或者客户已经先评价dm了。
						let evaluateInfo = await dbUtil.getOne({
							collection: 't3ds-store-evaluate',
							where: {
								signId: signInfo._id,
								storeId: signInfo.storeId,
								clientId: uid,
							}
						})
						if (!evaluateInfo) {
							await dbUtil.insert({
								collection: 't3ds-store-evaluate',
								data: {
									clientId: uid,
									public: true,
									scriptId: signInfo.scriptId,
									signId: signInfo._id,
									storeId: signInfo.storeId,
									dmId: signInfo.createBy,
									dmclient: rate,
									createTime: d.signTime,
									clientScriptRole: role,
									dmRemarkClient: note
								}
							}, transaction)
						} else {
							dbUtil.updateById({
								collection: 't3ds-store-evaluate',
								data: {
									_id: evaluateInfo._id,
									dmclient: rate,
									clientScriptRole: role,
									dmRemarkClient: note
								},
								transaction
							})
						}



						await dbUtil.updateById({
							collection: 't3ds-store-sign',
							data: data,
							transaction
						})
						transaction.commit();
					} catch (err) {
						transaction.rollback();
						return {
							errCode: 500,
							errMsg: err.message
						}
					}

					break
				}
			}
		}



		return {
			errCode: 0,
			errMsg: "修改成功！"
		}
	},

	async modifyRewards({
		id,
		uid,
		rewards
	}) {
		rewards = parseInt(rewards)
		if (isNaN(rewards) || !rewards || !uid || !id) {
			return errParams
		}
		let signInfo = await dbUtil.getById({
			collection: 't3ds-store-sign',
			id,
			field: {
				signDetail: true,
				createBy: true
			}
		})
		if (!signInfo) {
			return errResNotFound
		}
		if (signInfo.signDetail) {
			for (var i = 0; i < signInfo.signDetail.length; i++) {
				let d = signInfo.signDetail[i]
				if (d.uid == uid) {
					let transaction = await db.startTransaction()
					try {
						let integral = await dbUtil.getOne({
							collection: 't3ds-integral',
							where: {
								uid,
								storeId: signInfo.storeId
							}
						})
						await dbUtil.updateById({
							collection: 't3ds-integral',
							data: {
								_id: integral._id,
								balance: dbCmd.inc(-d.rewards),
								total: dbCmd.inc(-d.rewards)
							},
							transaction
						})
						await dbUtil.updateById({
							collection: 't3ds-integral',
							data: {
								_id: integral._id,
								balance: dbCmd.inc(rewards),
								total: dbCmd.inc(rewards)
							},
							transaction
						})
						// await transaction.collection('uni-id-users').doc(uid).update({
						// 	integral: dbCmd.inc(-d.rewards)
						// })
						await dbUtil.updateById({
							collection: 't3ds-store-sign',
							data: {
								_id: id,
								['signDetail.' + i]: {
									rewards
								}
							},
							transaction
						})
						let log = await dbUtil.getOne({
							collection: 't3ds-integral-log',
							where: {
								uid,
								bizId: id
							}
						})
						await dbUtil.updateById({
							collection: 't3ds-integral-log',
							data: {
								_id: log._id,
								balance: log.balance - d.rewards + rewards,
								income: rewards,
								updateBy: this.uid,
								updateTime: Date.now()
							},
							transaction
						})
						await transaction.commit()
					} catch (e) {
						await transaction.rollback()
						return {
							errCode: 502,
							errMsg: e.message
						}
					}

					break
				}
			}
		}
		return {
			errCode: 0,
			errMsg: "修改成功！"
		}
	},

	async modifyScript({
		id,
		scriptId
	}) {
		let signInfo = await dbUtil.getById({
			collection: 't3ds-store-sign',
			id,
			field: {
				signDetail: true,
				createBy: true
			}
		})
		if (!signInfo) {
			return errResNotFound
		}
		let script = await dbUtil.getById({
			collection: 't3ds-script',
			id: scriptId,
			field: {
				_id: true,
				name: true
			}
		})
		if (!script) {
			return errResNotFound
		}
		let transaction = await db.startTransaction()
		try {
			await dbUtil.updateById({
				collection: 't3ds-store-sign',
				data: {
					_id: id,
					scriptId
				},
				transaction
			})
			let logList = await dbUtil.getList({
				collection: 't3ds-integral-log',
				field: {
					_id: true,
				},
				where: {
					bizId: id,
					type: 1
				}
			})
			for (var i = 0; i < logList.length; i++) {
				let log = logList[i]
				await dbUtil.updateById({
					collection: 't3ds-integral-log',
					data: {
						_id: log._id,
						note: `《${script.name}》签到成功`
					},
					transaction
				})
			}
			await transaction.commit()
			return {
				errCode: 0,
				errMsg: "修改成功！"
			}
		} catch (e) {
			uniCloud.logger.error(e)
			await transaction.rollback()
			return {
				errCode: 502,
				errMsg: e.message
			}
		}
	},

	async modifyDM({
		id,
		dmId
	}) {
		let signInfo = await dbUtil.getById({
			collection: 't3ds-store-sign',
			id,
			field: {
				signDetail: true,
				createBy: true
			}
		})
		if (!signInfo) {
			return errResNotFound
		}
		let dm = await dbUtil.getById({
			collection: 'uni-id-users',
			id: dmId,
			field: {
				_id: true,
				name: true
			}
		})
		if (!dm) {
			return errResNotFound
		}
		let transaction = await db.startTransaction()
		try {
			await dbUtil.updateById({
				collection: 't3ds-store-sign',
				data: {
					_id: id,
					createBy: dmId
				},
				transaction
			})
			await transaction.commit()
			return {
				errCode: 0,
				errMsg: "修改成功！"
			}
		} catch (e) {
			uniCloud.logger.error(e)
			await transaction.rollback()
			return {
				errCode: 502,
				errMsg: e.message
			}
		}
	},

	async getQrcode(id) {
		if (!id) {
			return errParams
		}
		// let script = await dbUtil.getById({
		// 	collection: 't3ds-script',
		// 	id: scriptId,
		// 	field: {
		// 		_id: true
		// 	}
		// })
		// if (!script) {
		// 	return errResNotFound
		// }
		// let store = await dbUtil.getOne({
		// 	collection: 't3ds-store',
		// 	where: dbCmd.and([{
		// 		_id: storeId
		// 	}, dbCmd.or([{
		// 		employeeIds: dbCmd.all([this.uid])
		// 	}, {
		// 		uid: this.uid
		// 	}])])
		// })
		// if (!store) {
		// 	return {
		// 		code: 401,
		// 		msg: '无权限'
		// 	}
		// }
		let signInfo = await dbUtil.getById({
			collection: 't3ds-store-sign',
			id
		})
		if (!signInfo) {
			return errResNotFound
		}
		let script = await dbUtil.getById({
			collection: 't3ds-script',
			id: signInfo.scriptId
		})
		if (signInfo.signDetail) {
			let count = signInfo.signDetail.filter(item => {
				return !item.reverted
			}).length
			if (count >= script.limitPlayers) {
				return {
					errCode: 503,
					errMsg: '该剧本签到人数已达上限！'
				}
			}
		}
		// let data = await dbUtil.getOne({
		// 	collection: 't3ds-store-sign',
		// 	where: {
		// 		storeId,
		// 		scriptId,
		// 		uid: dbCmd.exists(false),
		// 		createBy: this.uid
		// 	}
		// })
		// if (!data) {
		// 	data = {
		// 		scriptId,
		// 		storeId,
		// 		deadline: Date.now() + 300000,
		// 		createBy: this.uid,
		// 		createTime: Date.now()
		// 	}
		// 	await dbUtil.insert({
		// 		collection: 't3ds-store-sign',
		// 		data
		// 	})
		// } else {
		// 	data.deadline = Date.now() + 300000
		// 	await dbUtil.updateById({
		// 		collection: 't3ds-store-sign',
		// 		data: {
		// 			_id: data._id,
		// 			deadline: data.deadline,
		// 			createTime: Date.now()
		// 		}
		// 	})
		// }
		return signInfo
	},
	async getMySignRecords({
		storeId = '',
		pageNum = 1,
		pageSize = 20
	}) {
		let total = 0
		let match = {
			'signDetail.uid': this.uid,
			'signDetail.reverted': false
		}
		if (storeId) {
			match.storeId = storeId
		}
		if (pageNum == 1) {
			let totalRes = await db.collection('t3ds-store-sign')
				.aggregate()
				.unwind('$signDetail')
				.match(match)
				.count('total')
				.end()
			total = totalRes.data.length ? totalRes.data[0].total : 0
			if (!total) {
				return {
					total,
					list: []
				}
			}
		}
		let res = await db.collection('t3ds-store-sign').aggregate()
			.unwind('$signDetail')
			.match(match)
			.lookup({
				from: 't3ds-script',
				let: {
					scriptId: '$scriptId'
				},
				pipeline: $.pipeline().match(dbCmd.expr($.and([
						$.eq(['$_id', '$$scriptId'])
					]))).project({
						_id: 0,
						name: 1,
						cover: 1
					})
					.done(),
				as: 'scriptInfo'
			})
			.lookup({
				from: 't3ds-store',
				let: {
					storeId: '$storeId'
				},
				pipeline: $.pipeline().match(dbCmd.expr($.and([
						$.eq(['$_id', '$$storeId'])
					]))).project({
						_id: 0,
						name: 1
					})
					.done(),
				as: 'storeInfo'
			})
			.lookup({
				from: 'uni-id-users',
				let: {
					uid: '$createBy'
				},
				pipeline: $.pipeline().match(dbCmd.expr($.and([
						$.eq(['$_id', '$$uid'])
					]))).project({
						_id: 0,
						nickname: 1,
						username: 1,
						avatar: 1
					})
					.done(),
				as: 'dmInfo'
			})
			.sort({
				'signDetail.signTime': -1
			})
			.skip((pageNum - 1) * pageSize)
			.limit(pageSize)
			.end()
		return {
			total,
			list: res.data
		}
	},
	async userSign(id, rewards) {
		rewards = parseInt(rewards)
		if (!id || isNaN(rewards) || rewards < 0 || rewards > 100) {
			return errParams
		}
		let transaction = await db.startTransaction()
		try {
			let signInfo = await dbUtil.getById({
				collection: 't3ds-store-sign',
				id,
				transaction
			})
			if (!signInfo) {
				await transaction.rollback()
				return errResNotFound
			}
			if (signInfo.fixedAwards && signInfo.fixedAwards > 0) {
				rewards = signInfo.fixedAwards
			}
			// let signCount = await dbUtil.getCount({
			// 	collection: 't3ds-store-sign',
			// 	where: {
			// 		'signDetail.uid': this.uid,
			// 		storeId: signInfo.storeId,
			// 		scriptId: signInfo.scriptId,
			// 		'signDetail.signTime': dbCmd.gte(todayStart).and(dbCmd.lte(todayEnd)),
			// 		'signDetail.reverted': false
			// 	}
			// })
			if (signInfo.signDetail) {
				for (var i = 0; i < signInfo.signDetail.length; i++) {
					let d = signInfo.signDetail[i]
					if (d.uid == this.uid && !d.reverted) {
						return {
							errCode: 503,
							errMsg: '您今天已签到该剧本，不能重复签到！'
						}
					}
				}
			}
			// let totalRes = await db.collection('t3ds-store-sign').aggregate()
			// 	.unwind('$signDetail')
			// 	.match({
			// 		'signDetail.uid': this.uid,
			// 		storeId: signInfo.storeId,
			// 		scriptId: signInfo.scriptId,
			// 		// 'signDetail.signTime': dbCmd.gte(todayStart).and(dbCmd.lte(todayEnd)),
			// 		'signDetail.reverted': false
			// 	})
			// 	.count('total')
			// 	.end()
			// let signCount = totalRes.data.length ? totalRes.data[0].total : 0
			// if (signCount > 0) {
			// 	await transaction.rollback()
			// 	return {
			// 		errCode: 503,
			// 		errMsg: '您今天已签到该剧本，不能重复签到！'
			// 	}
			// }
			let script = await dbUtil.getById({
				collection: 't3ds-script',
				id: signInfo.scriptId,
				field: {
					name: true,
					limitPlayers: true
				}
			})
			if (signInfo.signDetail) {
				let count = signInfo.signDetail.filter(item => {
					return !item.reverted
				}).length
				if (count >= script.limitPlayers) {
					await transaction.rollback()
					return {
						errCode: 505,
						errMsg: '该剧本签到人数已达上限！'
					}
				}
			}
			let totalRes = await db.collection('t3ds-store-sign').aggregate()
				.unwind('$signDetail')
				.match({
					'signDetail.uid': this.uid,
					storeId: signInfo.storeId,
					'signDetail.signTime': dbCmd.gte(todayStart).and(dbCmd.lte(todayEnd)),
					'signDetail.reverted': false
				})
				.count('total')
				.end()
			let signCount = totalRes.data.length ? totalRes.data[0].total : 0
			if (signCount > 2) {
				await transaction.rollback()
				return {
					errCode: 504,
					errMsg: '您今天的签到次数已达上限！'
				}
			}
			//2023-12-28，新增用户签到字段{isEvaluate：是否评价，rate：dm给客户打分}
			await dbUtil.updateById({
				collection: 't3ds-store-sign',
				data: {
					_id: signInfo._id,
					signDetail: dbCmd.push({
						uid: this.uid,
						signTime: Date.now(),
						rewards,
						reverted: false,
						isEvaluate: false,
						rate: 0
					})
				},
				transaction
			})
			// let user = await dbUtil.getById({
			// 	collection: 'uni-id-users',
			// 	id: this.uid,
			// 	field: {
			// 		integral: true
			// 	}
			// })
			// await dbUtil.updateById({
			// 	collection: 'uni-id-users',
			// 	data: {
			// 		_id: this.uid,
			// 		integral: dbCmd.inc(rewards)
			// 	},
			// 	transaction
			// })
			let integral = await dbUtil.getOne({
				collection: 't3ds-integral',
				where: {
					uid: this.uid,
					storeId: signInfo.storeId
				}
			})
			if (integral) {
				await dbUtil.updateById({
					collection: 't3ds-integral',
					data: {
						_id: integral._id,
						balance: dbCmd.inc(rewards),
						total: dbCmd.inc(rewards)
					},
					transaction
				})
			} else {
				await dbUtil.insert({
					collection: 't3ds-integral',
					data: {
						uid: this.uid,
						storeId: signInfo.storeId,
						balance: rewards,
						total: rewards
					},
					transaction
				})
			}
			await dbUtil.insert({
				collection: 't3ds-integral-log',
				data: {
					uid: this.uid,
					storeId: signInfo.storeId,
					type: 1,
					balance: integral ? (integral.balance || 0) + rewards : rewards,
					income: rewards,
					createTime: Date.now(),
					createBy: this.uid,
					bizId: id,
					note: `《${script.name}》签到成功`
				},
				transaction
			})
			await transaction.commit()
			return 0
		} catch (err) {
			await transaction.rollback()
			return {
				errCode: 500,
				errMsg: err.message
			}
		}
	},
	async revert(id, uid) {
		if (!id || !uid) {
			return errParams
		}
		let transaction = await db.startTransaction()
		try {
			let signInfo = await dbUtil.getById({
				collection: 't3ds-store-sign',
				id,
				transaction
			})
			if (!signInfo) {
				await transaction.rollback()
				return errResNotFound
			}
			if (!signInfo.signDetail) {
				await transaction.rollback()
				return errResNotFound
			}
			let detail = null
			for (var i = 0; i < signInfo.signDetail.length; i++) {
				let d = signInfo.signDetail[i]
				if (d.uid == uid) {
					detail = d
				}
			}
			if (!detail) {
				await transaction.rollback()
				return errResNotFound
			}
			if (detail.reverted) {
				await transaction.rollback()
				return {
					errCode: 502,
					errMsg: '已撤销'
				}
			}
			detail.reverted = true
			await dbUtil.updateById({
				collection: 't3ds-store-sign',
				data: {
					_id: id,
					signDetail: signInfo.signDetail
				}
			})
			// let user = await dbUtil.getById({
			// 	collection: 'uni-id-users',
			// 	id: uid,
			// 	field: {
			// 		integral: true
			// 	}
			// })
			// await dbUtil.updateById({
			// 	collection: 'uni-id-users',
			// 	data: {
			// 		_id: uid,
			// 		integral: dbCmd.inc(-detail.rewards)
			// 	},
			// 	transaction
			// })
			let integral = await dbUtil.getOne({
				collection: 't3ds-integral',
				where: {
					uid,
					storeId: signInfo.storeId
				}
			})
			await dbUtil.updateById({
				collection: 't3ds-integral',
				data: {
					_id: integral._id,
					balance: dbCmd.inc(-detail.rewards),
					total: dbCmd.inc(-detail.rewards)
				},
				transaction
			})
			let log = await dbUtil.getOne({
				collection: 't3ds-integral-log',
				where: {
					uid,
					bizId: id
				}
			})
			
			await dbUtil.removeById({
				collection: 't3ds-integral-log',
				id: log._id,
				transaction
			})
			
			await db.collection('t3ds-store-evaluate').where({
				signId: dbCmd.eq(id)
			}).remove();
			
			await transaction.commit()
			return 0
		} catch (err) {
			await transaction.rollback()
			return {
				errCode: 500,
				errMsg: err.message
			}
		}
	},
	async getMyIntegral(storeId) {
		if (!storeId) {
			let res = await db.collection('t3ds-integral')
				.aggregate()
				.match({
					uid: this.uid
				})
				.group({
					_id: '$uid',
					total: $.sum('$balance')
				})
				.end()
			return res.data.length ? res.data[0].total : 0
		}
		let res = await dbUtil.getOne({
			collection: 't3ds-integral',
			where: {
				storeId,
				uid: this.uid
			}
		})
		if (res) {
			return res.balance
		}
		return 0
	},
	async remove(id) {
		if (!id) {
			return errParams
		}
		let signInfo = await dbUtil.getById({
			collection: 't3ds-store-sign',
			id,
			field: {
				signDetail: true
			}
		})
		if (!signInfo) {
			return errResNotFound
		}
		if (signInfo.signDetail) {
			let count = signInfo.signDetail.filter(item => {
				return !item.reverted
			}).length
			if (count > 0) {
				return {
					errCode: 502,
					errMsg: '无法删除已签到的剧本！'
				}
			}
		}
		let transaction = await db.startTransaction()
		try {
			await dbUtil.removeById({
				collection: 't3ds-store-sign',
				id,
				transaction
			})
			//删除跟这个剧本关联的评价
			await db.collection('t3ds-store-evaluate').where({
				signId: dbCmd.eq(id)
			}).remove();
			transaction.commit()
			return {
				errCode: 0,
				errMsg: '删除成功！'
			}
		} catch (e) {
			transaction.rollback()
			return {
				errCode: 502,
				errMsg: '删除失败！',
			}
		}

	},
	//剧本签到评价
	async scriptEvaluation({
		environment,
		serve,
		script,
		clientdm,
		clientfeedback,
		signId
	}) {
		const signInfo = await dbUtil.getById({
			collection: 't3ds-store-sign',
			id: signId
		})
		if (!signInfo) {
			return errResNotFound
		}
		const evaluateInfo = await dbUtil.getOne({
			collection: 't3ds-store-evaluate',
			where: {
				signId: signId,
				clientId: this.uid
			}
		})
		const scriptInfo = await dbUtil.getById({
			collection: 't3ds-script',
			id: signInfo.scriptId,
			field: {
				name: true,
				limitPlayers: true
			}
		})
		let transaction = await db.startTransaction()
		try {
			for (var i = 0; i < signInfo.signDetail.length; i++) {
				let d = signInfo.signDetail[i]
				if (d.uid == this.uid) {
					await dbUtil.updateById({
						collection: 't3ds-store-sign',
						data: {
							_id: signInfo._id,
							['signDetail.' + i]: {
								isEvaluate: true
							}
						},
						transaction
					});
					if (evaluateInfo != null) {
						dbUtil.updateById({
							collection: 't3ds-store-evaluate',
							data: {
								_id: evaluateInfo._id,
								clientdm: clientdm,
								clientfeedback: clientfeedback,
								environment: environment,
								script: script,
								serve: serve,
								evaluateTime: Date.now()
							},
							transaction
						})
					} else {
						await dbUtil.insert({
							collection: 't3ds-store-evaluate',
							data: {
								clientId: this.uid,
								clientdm: clientdm,
								clientfeedback: clientfeedback,
								environment: environment,
								public: true,
								script: script,
								scriptId: signInfo.scriptId,
								serve: serve,
								signId: signInfo._id,
								storeId: signInfo.storeId,
								dmId: signInfo.createBy,
								createTime: d.signTime,
								evaluateTime: Date.now()
							},
							transaction
						})
					}



					break
				}
			}

			await dbUtil.insert({
				collection: 't3ds-integral-log',
				data: {
					uid: this.uid,
					storeId: signInfo.storeId,
					type: 3,
					createTime: Date.now(),
					createBy: this.uid,
					note: `评价成功《${scriptInfo.name}》`
				},
				transaction
			})
			await transaction.commit()
			return 0
		} catch (err) {
			await transaction.rollback()
			return {
				errCode: 500,
				errMsg: err.message
			}
		}
	},




	async updateStoreSigner(id, top, topTime) {

		if (top) {

			dbUtil.updateById({
				collection: 't3ds-store-sign',
				data: {
					_id: id,
					top: top,
					topTime: topTime
				}
			})
		} else {
			dbUtil.updateById({
				collection: 't3ds-store-sign',
				data: {
					_id: id,
					top: top,
					topTime: dbCmd.remove()
				}
			})
		}




	}
}